package com.elai.tags.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

import com.elai.tags.domain.BasePortraitTemplateItems;
import com.elai.tags.domain.BaseUserPortraitCompents;
import com.elai.tags.service.IBasePortraitTemplateItemsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elai.common.annotation.Log;
import com.elai.common.core.controller.BaseController;
import com.elai.common.core.domain.AjaxResult;
import com.elai.common.enums.BusinessType;
import com.elai.tags.domain.BasePortraitTemplate;
import com.elai.tags.service.IBasePortraitTemplateService;
import com.elai.common.utils.poi.ExcelUtil;
import com.elai.common.core.page.TableDataInfo;

/**
 * 画像模板Controller
 * 
 * @author elai
 * @date 2021-10-09
 */
@RestController
@RequestMapping("/tag/template")
public class BasePortraitTemplateController extends BaseController
{
    @Autowired
    private IBasePortraitTemplateService basePortraitTemplateService;

    @Autowired
    private IBasePortraitTemplateItemsService basePortraitTemplateItemsService;

    /**
     * 查询画像模板列表
     */
    @PreAuthorize("@ss.hasPermi('tag:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasePortraitTemplate basePortraitTemplate)
    {
        startPage();
        List<BasePortraitTemplate> list = basePortraitTemplateService.selectBasePortraitTemplateList(basePortraitTemplate);
        return getDataTable(list);
    }

    /**
     * 导出画像模板列表
     */
    @PreAuthorize("@ss.hasPermi('tag:template:export')")
    @Log(title = "画像模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BasePortraitTemplate basePortraitTemplate)
    {
        List<BasePortraitTemplate> list = basePortraitTemplateService.selectBasePortraitTemplateList(basePortraitTemplate);
        ExcelUtil<BasePortraitTemplate> util = new ExcelUtil<BasePortraitTemplate>(BasePortraitTemplate.class);
        return util.exportExcel(list, "画像模板数据");
    }

    /**
     * 获取画像模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('tag:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        BasePortraitTemplate  template= basePortraitTemplateService.selectBasePortraitTemplateById(id);
        return AjaxResult.success(template);
    }

    @GetMapping(value = "/getPortraitTemplate/{id}")
    public AjaxResult getPortraitTemplate(@PathVariable("id") Long id)
    {
        BasePortraitTemplate  template= basePortraitTemplateService.selectBasePortraitTemplateById(id);
        BasePortraitTemplateItems item=new BasePortraitTemplateItems();
        item.setTemplateId(template.getId());
        List<BasePortraitTemplateItems> itemList=basePortraitTemplateItemsService.selectBasePortraitTemplateItemsList(item);
        List compentsList=new ArrayList();
        itemList.stream().forEach(citem->{
            BaseUserPortraitCompents compent=new BaseUserPortraitCompents();
            compent.setComponentName(citem.getComponentName());
            compent.setPortraitType(citem.getAnalyticsType());
            compent.setRemark(citem.getRemark());
            compent.setDateUnit(citem.getDateUnit());
            compent.setChartType(citem.getChartType());
            compent.setQueryRule(citem.getQueryRule());
            compent.setSortNo(citem.getSortNo());
            compent.setParamField1(citem.getParamField1());
            compent.setParamField2(citem.getParamField2());
            compentsList.add(compent);
            /*if("bar".equals(citem.getChartType())){
                List ydata=new ArrayList();
                ydata.add("衬衫");
                ydata.add("羊毛衫");
                ydata.add("雪纺衫");
                ydata.add("裤子");
                ydata.add("高跟鞋");
                List xdata=new ArrayList();
                Map d1=new HashMap();
                d1.put("name","Search Engine");
                d1.put("value",1048);
                Map d2=new HashMap();
                d2.put("name","Search Engine");
                d2.put("value",1048);
                Map d3=new HashMap();
                d3.put("name","Search Engine");
                d3.put("value",1048);
                Map d4=new HashMap();
                d4.put("name","Search Engine");
                d4.put("value",1048);
                Map d5=new HashMap();
                d5.put("name","Search Engine");
                d5.put("value",1048);
                xdata.add(d1);
                xdata.add(d2);
                xdata.add(d3);
                xdata.add(d4);
                xdata.add(d5);
                citem.setXdata(xdata);
                citem.setYdata(ydata);
            }
            else if("ring".equals(citem.getChartType())){
                List xdata=new ArrayList();
                Map d1=new HashMap();
                d1.put("name","Search Engine");
                d1.put("value",1048);
                Map d2=new HashMap();
                d2.put("name","Search Engine");
                d2.put("value",1048);
                Map d3=new HashMap();
                d3.put("name","Search Engine");
                d3.put("value",1048);
                Map d4=new HashMap();
                d4.put("name","Search Engine");
                d4.put("value",1048);
                Map d5=new HashMap();
                d5.put("name","Search Engine");
                d5.put("value",1048);
                xdata.add(d1);
                xdata.add(d2);
                xdata.add(d3);
                xdata.add(d4);
                xdata.add(d5);
                citem.setXdata(xdata);
            }else if("pie".equals(citem.getChartType())){
                List xdata=new ArrayList();
                Map d1=new HashMap();
                d1.put("name","Search Engine");
                d1.put("value",1048);
                Map d2=new HashMap();
                d2.put("name","Search Engine");
                d2.put("value",1048);
                Map d3=new HashMap();
                d3.put("name","Search Engine");
                d3.put("value",1048);
                Map d4=new HashMap();
                d4.put("name","Search Engine");
                d4.put("value",1048);
                Map d5=new HashMap();
                d5.put("name","Search Engine");
                d5.put("value",1048);
                xdata.add(d1);
                xdata.add(d2);
                xdata.add(d3);
                xdata.add(d4);
                xdata.add(d5);
                citem.setXdata(xdata);
            }
            else if("cate".equals(citem.getChartType())){
                // xdata: ['[20,40]','[40,60]','[60,80]'],
                List xdata=new ArrayList();
                xdata.add(new Integer[]{20,40});
                xdata.add(new Integer[]{40,60});
                xdata.add(new Integer[]{60,80});
                citem.setXdata(xdata);

                List ydata=new ArrayList();
                //ydata: ['张三','李四','王五'],
                ydata.add("张三");
                ydata.add("李四");
                ydata.add("王五");
                citem.setYdata(ydata);

                //data: [[0, 0, 20], [0, 1, 50], [0, 2, 80], [1, 0, 30], [1, 1, 60], [1, 2, 70],[2, 0, 40], [2, 1, 70], [2, 2, 30],]
                List zdata=new ArrayList();
                zdata.add(new Integer[]{0, 0, 20});
                zdata.add(new Integer[]{0, 1, 50});
                zdata.add(new Integer[]{0, 2, 80});
                zdata.add(new Integer[]{1, 0, 30});
                zdata.add(new Integer[]{1, 1, 60});
                zdata.add(new Integer[]{1, 2, 70});
                zdata.add(new Integer[]{2, 0, 40});
                zdata.add(new Integer[]{2, 1, 70});
                zdata.add(new Integer[]{2, 2, 30});
                citem.setZdata(zdata);
            }
            else if("pillar".equals(citem.getChartType())){
                List xdata=new ArrayList();
                Map d1=new HashMap();
                d1.put("name","Search Engine");
                d1.put("value",1048);
                Map d2=new HashMap();
                d2.put("name","Search Engine");
                d2.put("value",1048);
                Map d3=new HashMap();
                d3.put("name","Search Engine");
                d3.put("value",1048);
                Map d4=new HashMap();
                d4.put("name","Search Engine");
                d4.put("value",1048);
                Map d5=new HashMap();
                d5.put("name","Search Engine");
                d5.put("value",1048);
                xdata.add(d1);
                xdata.add(d2);
                xdata.add(d3);
                xdata.add(d4);
                xdata.add(d5);
                citem.setXdata(xdata);
            }*/
        });
        template.setBasePortraitTemplateItemsList(itemList);
        return AjaxResult.success(template);
    }

    /**
     * 新增画像模板
     */
    @PreAuthorize("@ss.hasPermi('tag:template:add')")
    @Log(title = "画像模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasePortraitTemplate basePortraitTemplate)
    {
        return toAjax(basePortraitTemplateService.insertBasePortraitTemplate(basePortraitTemplate));
    }

    /**
     * 修改画像模板
     */
    @PreAuthorize("@ss.hasPermi('tag:template:edit')")
    @Log(title = "画像模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasePortraitTemplate basePortraitTemplate)
    {
        return toAjax(basePortraitTemplateService.updateBasePortraitTemplate(basePortraitTemplate));
    }

    /**
     * 删除画像模板
     */
    @PreAuthorize("@ss.hasPermi('tag:template:remove')")
    @Log(title = "画像模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basePortraitTemplateService.deleteBasePortraitTemplateByIds(ids));
    }
}
